<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">常见问题配置</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <!-- <div v-if="meta.indexOf('MemberTagAdd') > -1" class="doumee-table-buttons"> -->
    <div v-if="meta.indexOf('MemberTagAdd') > -1" class="doumee-table-buttons">
      <el-button type="primary" @click="handleEdit()">{{
        translate("add")
      }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('tagName')"
        prop="name"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('memberNum')"
        prop="memberNum"
        min-width="80"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.memberNum || "-" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('creator')"
        prop="creatorName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        :label="translate('handle')"
        align="center"
        width="140"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <!-- <el-button v-if="meta.indexOf('NoviceHelpEdit') > -1" v-preventReClick type="text" @click="handleEdit(scope.row)">{{translate('edit')}}</el-button> -->
            <el-button
              v-if="meta.indexOf('MemberTagUpdate') > -1"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
            >{{ translate("edit") }}</el-button>
            <el-button
              v-if="meta.indexOf('MemberTagDel') > -1"
              v-preventReClick
              type="text"
              style="color: red"
              @click="handleDel(scope.row.id)"
            >{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @success="getList()"
      @close="isShowEdit = false"
    />
  </div>
</template>

<script>
import {
  memberTagList,
  memberTagDel
} from '@/api'
import { queryFormConfig } from './config'

import Edit from './edit.vue'
export default {
  name: 'TagOption',
  components: {
    Edit
  },
  data() {
    return {
      queryFormConfig,
      cgList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: true,
      meta: [],
      filters: {},

      isShowEdit: false
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleEdit(row) {
      this.isShowEdit = true
      this.$nextTick(() => {
        this.$refs.EditRef.param = {}
        if (row && row.id) {
          this.$refs.EditRef.param = { ...row }
        }
        this.$refs.EditRef.isShowModal = true
      })
    },
    handleDel(id) {
      const that = this
      this.$confirm(this.translate('tagDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          memberTagDel({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success(this.translate('delScs'))
            that.getList()
          })
        })
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
      }

      this.listLoading = true
      memberTagList({
        pagination,
        param
      }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
